$(function () {
    cksh_init();
})


function cksh_init() {
    //公司加  载
    $("#cksh-toolbar [name='_gs']").gsjgtree(1);
    //$("[name='_gs']").jgbmtree($("[name='_gs']"), 1, '', '');
    //客户加载
    $("#cksh-toolbar [name='_kh']").qyjgzcombobox();
    //工程加载
    $("#cksh-toolbar [name='_ck']").ckcombobox();
    var options = getckshGridOption();
    $('#cksh-grid').datagrid(options);
}
function cksh_search() {
    var xsdh = $("#cksh-toolbar [name='_xsdh']").val();
    var kh = $("#cksh-toolbar [name='_kh']").val();
    var ck = $("#cksh-toolbar [name='_ck']").val();
    var zdrqStart = $('#zdrqStart').datebox('getValue');
    var zdrqEnd = $('#zdrqEnd').datebox('getValue');
    var cksjStart = $('#cksjStart').datebox('getValue');
    var cksjEnd = $('#cksjEnd').datebox('getValue');
    var xsgs = $("#cksh-toolbar [name='_gs']").val();
    var yck = $("#cksl_yck").combobox('getValue');
    var yps = $("#cksl_yps").combobox('getValue');
    var wzbm = $("#cksh-toolbar [name='queryCksh.wzbm']").val();
    var options = $.extend(getckshGridOption(),
        {queryParams:{xsdh:xsdh, khId:kh, ckId:ck, zdrqStart:zdrqStart, zdrqEnd:zdrqEnd, xsgsId:xsgs, yck:yck, yps:yps, wzbm:wzbm, cksjStart:cksjStart, cksjEnd:cksjEnd}});
    $('#cksh-grid').datagrid(options);
}


function cksh_editItem(title) {
    var record = $("#cksh-grid").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要审核的记录!", "info");
        return;
    }
    if (record.fyzr_z == '配送' && record.yps == false) {
        $.messager.alert("提示", "此单据未配送!请先配送!", "info");
        return;
    }
    var href = $.webPath() + "/kcgl/cksh/cksh_content?xsdId=" + record.id;
    cksh_showDialog(title, href);
}

//新增、修改弹出界面
var cksh_win;
function cksh_showDialog(title, href) {

    var button = [
        {text:'保存', handler:cksh_saveItem}
    ];

    cksh_win = $.window({
        title:title,
        iconCls:'icon-add2',
        url:href,
        width:1024,
        height:480,
        buttons:button
    });
}
//保存信息
function cksh_saveItem() {
    cksh_win.mask();
    $("#cksh_mx").edatagrid('saveRow');
    $('#ckshForm').tabs('select', "销售信息");
    $("#ckshForm").form('submit', {
        url:$.webPath() + "/kcgl/cksh/check_cksh",
        onSubmit:function (param) {
            var data = $('#cksh_mx').edatagrid("getData");
            if (!data.rows.length) {
                $('#ckshForm').tabs('select', "销售明细");
                $.messager.alert("提示", "请填写销售明细!", "waring");
                cksh_win.mask("hide");
                return false;
            }
            var arrayPrefix = 'xsd.xsmxList';
            //销售明细
            var paramString = "{";
            //销售明细
            for (var i = 0; i < data.rows.length; i++) {
                {
                    var objectString = $.toJSONStringSubPrefix(data.rows[i], arrayPrefix + "[" + i + "].", ['id', 'jzfs.id', 'jzfs.z', 'hsj', 'bhsj', 'bz', 'ck_js', 'ck_sbsl', 'sbsl', 'js', 'kh.id']);
                    paramString = paramString + objectString.substring(1, objectString.length - 1) + ",";
                }
            }
            //销售明细
            if (data.rows && data.rows.length > 0)
                paramString = paramString.substring(0, paramString.length - 1);
            paramString += "}";
            $.extend(param, $.parseJSON(paramString));
            return $(this).form('validate');
        },
        success:function (data) {
            cksh_win.mask("hide");
            var obj = eval('(' + data + ')');
            if (obj.statusCode == '200') {
                $(cksh_win).window('close');
                $('#cksh-grid').datagrid('reload');
                $('#cksh_layout').layout('panel', 'center').panel('refresh');
                $.messager.show({title:'提示框', msg:obj.message, timeout:3000, showType:'slide'});
            } else {
                $.messager.alert('提示', obj.message, 'info');
            }
        }
    });
}


function getckshGridOption() {
    var options = {
        url:$.webPath() + "/kcgl/cksh/ckshList",
        toolbar:"#cksh-toolbar",
        fitColumns:false,
        singleSelect:true,
        pageNumber:1,
        pagination:true,
        showFooter:true,
        fit:true,
        rownumbers:true,
        onLoadSuccess:function (target) {
            $("#cksh-edit").css('display', 'none');
            $(this).datagrid('selectRow', 0);
        },
        loadFilter:function (data) {
            $.each(data.rows, function () {
                if (this.zt) {
                    this.zt = this.zt == 'yly' ? "已完结" : this.zt == 'yzz' ? "已终止" : this.zt == 'ytj' ? "已提交" : this.zt == "ysp" ? "已审批" : "已保存";
                    this.lx = this.xslx == 'kcls' ? '库存零售' : this.xslx == 'htxs' ? '合同销售' : this.xslx == 'nbxs' ? '内部销售' : '保供销售';
                }
            })
            return data;
        },
        onSelect:function (rowIndex, rowData) {
            if (rowData && rowData.ckshbz == false) {
                $("#cksh-edit").css('display', '');
            }
            if (rowData && rowData.ckshbz == true) {
                $("#cksh-edit").css('display', 'none');
            }
//            if (rowData && $("#cksh_layout #cksh_id") && $("#cksh_layout #cksh_id").val() != rowData.id) {
//                $('#cksh_layout').layout('remove', 'center');
//                $('#cksh_layout').layout('add', {
//                    region: 'center',
//                    split: true,
//                    href: $.webPath() + '/kcgl/cksh/cksh_detail?xsdId=' + rowData.id
//                });
//            } else {
//                $('#cksh_layout').layout('remove', 'center');
//            }

        }/*, onDblClickCell: function (index, field, value) {
         if (field == "xsdh") {
         cksh_editItem('审核');
         }
         }*/, rowStyler:function (index, row) {
            if (row.ckshbz == true) {
                return 'background-color:rgba(255, 130, 87, 0.64);';
            }
        }
    }
    var columns = {
        frozenColumns:[
            [
                {field:'xsdh', title:'销售单号', align:'center', width:150, formatter:ckshCell}
            ]
        ],
        columns:[
            [
                {field:'lx', title:'销售类型', align:'center', width:70},
                {field:'sxsj', title:'开单日期', align:'center', width:80},
                {field:'kh_qc', title:'客户名称', align:'center', width:200},
                {field:'xssl', title:'开单重量', align:'right', width:130,
                    formatter:function (value) {
                        var val = formatSlForHtml(value);
                        return val;
                    }},
                {field:'ck_sbsl', title:'出库重量', align:'right', width:130,
                    formatter:function (value) {
                        var val = formatSlForHtml(value);
                        return val;
                    }},
                {field:'thck_mc', title:'提货仓库', align:'center', width:150},
//                {field:'fyzr_z', title:'配送类型', align:'center', width:70},
                {field:'yps', title:'配送状态', width:100, align:'center',
                    formatter:function (value, rowData, rowIndex) {
                        if (rowData.fyzr_z == '自提') {
                            return '自提';
                        } else if (rowData.fyzr_z == '配送' && value == false) {
                            return '<a style="color:red">未配送</a>';
                        } else if (rowData.fyzr_z == '配送' && value == true) {
                            return '已配送';
                        }
                    }},
                {field:'yck', title:'状态', width:100, align:'center',
                    formatter:function (value) {
                        if (value == false)
                            return '未出库';
                        if (value == true)
                            return '已出库';
                    }},
                {field:'dsyfje', title:'配送费用(元)', width:130, align:'right',
                    formatter:function (value) {
                        var val = formatJeForHtml(value);
                        return val;
                    }
                },
                {field:'xsgs_qc', title:'销售公司', align:'center', width:150},
                {field:'zdr', title:'制单人', align:'center', width:100},
                {field:'date_created', title:'制单时间', align:'center', width:80},
                {field:"ckr", title:"出库人", align:'center', width:100},
                {field:"cksj", title:"出库时间", align:'center', width:100}
//            {field:'ckshbz', title:'审核标志', width:100,
//                formatter:function (value) {
//                    if (value == false)
//                        return '未审核';
//                    if (value == true)
//                        return '已审核';
//                }}
            ]
        ], onLoadSuccess:function () {
            $(this).datagrid('selectRow', 0);
            var row = $('#cksh-grid').datagrid('getSelected');
            if (row && row.ckshbz == true) {
                $(this).datagrid("showColumn", 'cksj');
                $(this).datagrid("showColumn", 'ckr');
            } else if (row && !row.ckshbz == true) {
                $(this).datagrid("hideColumn", 'cksj');
                $(this).datagrid("hideColumn", 'ckr');
            } else {
                $(this).datagrid("hideColumn", 'cksj');
                $(this).datagrid("hideColumn", 'ckr');
            }

            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        }
    }
    options = $.extend(options, columns);
    return options;
}